package com.love.marriage.eventInfo.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.love.marriage.common.enums.DefaultAmountTypeEnum;
import com.love.marriage.common.vo.SecurityUser;
import com.love.marriage.defaultAmountConfig.service.DefaultAmountConfigService;
import com.love.marriage.defaultAmountConfig.vo.DefaultAmountConfigVo;
import com.love.marriage.eventInfo.entity.EventInfo;
import com.love.marriage.eventInfo.mapper.EventInfoMapper;
import com.love.marriage.eventInfo.service.EventInfoService;
import com.love.marriage.eventInfo.vo.EventInfoVo;
import com.love.marriage.eventRegistrationInfo.mapper.EventRegistrationInfoMapper;
import com.love.marriage.eventRegistrationInfo.vo.EventRegistrationInfoVo;
import com.love.marriage.oss.service.FileInfoService;
import com.love.marriage.oss.vo.FileInfoVo;
import com.love.marriage.utils.StringUtils;
import com.love.marriage.utils.UserUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * description:  活动表服务实现类
 * author:       alex
 * createDate:   2024-10-15 04:26:31
 * version:      1.0.0
 */
@Service
@RequiredArgsConstructor
public class EventInfoServiceImp extends ServiceImpl<EventInfoMapper, EventInfo> implements EventInfoService {

    private final EventInfoMapper eventInfoMapper;

    private final FileInfoService fileInfoService;

    private final DefaultAmountConfigService defaultAmountConfigService;

    private final EventRegistrationInfoMapper eventRegistrationInfoMapper;

    @Override
    public Page<EventInfoVo> getPage(Long pageNum, Long pageSize, EventInfoVo eventInfoVo) {
        SecurityUser loginUser = UserUtils.getLoginUser();
        Long curUserId = null;
        if (loginUser != null) {
            curUserId = loginUser.getId();
        }
        Page<EventInfoVo> page = new Page<>(pageNum == null ? 1 : pageNum, pageSize == null ? 10 : pageSize);
        Page<EventInfoVo> res = eventInfoMapper.getPage(page, eventInfoVo, curUserId);
        if (res.getRecords() != null) {
            res.getRecords().forEach(eventInfoVo1 -> {
                if (StringUtils.isNotBlank(eventInfoVo1.getImages())) {
                    try {
                        List<Long> imageIds = Arrays.stream(eventInfoVo1.getImages().split(",")).map(Long::parseLong).collect(Collectors.toList());
                        List<FileInfoVo> fileInfoVoList = fileInfoService.getFileInfo(imageIds);
                        eventInfoVo1.setImagesList(fileInfoVoList);
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                }
            });

        return res;
        }
        return res;
    }

    @Override
    public EventInfoVo queryEventInfo(Long id) throws Exception {
        EventInfoVo eventInfoVo = eventInfoMapper.queryEventInfo(id);
        if (eventInfoVo == null) {
            throw new Exception("活动不存在！");
        }
        if (StringUtils.isNotBlank(eventInfoVo.getImages())) {
            try {
                List<Long> imageIds = Arrays.stream(eventInfoVo.getImages().split(",")).map(Long::parseLong).collect(Collectors.toList());
                List<FileInfoVo> fileInfoVoList = fileInfoService.getFileInfo(imageIds);
                eventInfoVo.setImagesList(fileInfoVoList);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        if (eventInfoVo.getFeeAmount() == null) {
            DefaultAmountConfigVo defaultAmountConfigVo = defaultAmountConfigService.queryDefaultAmountConfigByCode(DefaultAmountTypeEnum.JOIN_EVENT.getCode());
            eventInfoVo.setFeeAmount(defaultAmountConfigVo.getAmount());
        }
        EventRegistrationInfoVo eventRegistrationInfoVo = new EventRegistrationInfoVo();
        eventRegistrationInfoVo.setEventId(eventInfoVo.getId());
        List<EventRegistrationInfoVo> list = eventRegistrationInfoMapper.getList(eventRegistrationInfoVo);
        if (list != null && !list.isEmpty()) {
            list.forEach(eventRegistrationInfoVo1 -> {
               if (eventRegistrationInfoVo1.getUserInfoVo() != null &&
                       eventRegistrationInfoVo1.getUserInfoVo().getAvatar() != null) {
                   try {
                       FileInfoVo avatar = fileInfoService.queryFileInfo(eventRegistrationInfoVo1.getUserInfoVo().getAvatar());
                       eventRegistrationInfoVo1.getUserInfoVo().setPersonAvatar(avatar);
                   } catch (Exception e) {
                       throw new RuntimeException(e);
                   }
               }
            });
        }
        eventInfoVo.setEventRegistrationInfoVos(list);
        return eventInfoVo;
    }

    @Override
    public EventInfoVo addEventInfo(EventInfoVo eventInfoVo) {
        EventInfo eventInfo = new EventInfo();
        BeanUtils.copyProperties(eventInfoVo, eventInfo);
        eventInfoMapper.insert(eventInfo);
        EventInfoVo res = new EventInfoVo();
        BeanUtils.copyProperties(eventInfo, res);
        return res;
    }

    @Override
    public EventInfoVo updateEventInfo(EventInfoVo eventInfoVo) {
        EventInfo eventInfo = new EventInfo();
        BeanUtils.copyProperties(eventInfoVo, eventInfo);
        eventInfoMapper.updateById(eventInfo);
        return eventInfoVo;
    }

    @Override
    public Boolean deleteEventInfo(String ids) {
        if(StringUtils.isEmpty(ids)) {
            return true;
        }
        List<String> idArr = Arrays.asList(ids.split(","));
        eventInfoMapper.deleteBatchIds(idArr);
        return true;
    }
}
